package cn.tedu.ximart.server.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc

public class Knife4jConfiguration {

        /**
         * 【重要】指定Controller包路径
         */
        private String basePackage = "cn.tedu.ximart.server";
        /**
         * 分组名称
         */
        private String groupName = "product";
        /**
         * 主机名
         */
        private String host = "http://java.tedu.cn";
        /**
         * 标题
         */
        private String title = "酷鲨商城在线API文档--商品管理";
        /**
         * 简介
         */
        private String description = "酷鲨商城在线API文档--商品管理";
        /**
         * 服务条款URL
         */
        private String termsOfServiceUrl = "http://www.apache.org/licenses/LICENSE-2.0";
        /**
         * 联系人
         */
        private String contactName = "Java教学研发部";
        /**
         * 联系网址
         */
        private String contactUrl = "http://java.tedu.cn";
        /**
         * 联系邮箱
         */
        private String contactEmail = "java@tedu.cn";
        /**
         * 版本号
         */
        private String version = "1.0.0";

        @Autowired
        private OpenApiExtensionResolver openApiExtensionResolver;

        @Bean
        public Docket docket() {
            String groupName = "1.0.0";
            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .host(host)
                    .apiInfo(apiInfo())
                    .groupName(groupName)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(basePackage))
                    .paths(PathSelectors.any())
                    .build()
                    .extensions(openApiExtensionResolver.buildExtensions(groupName));
            return docket;
        }

        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title(title)
                    .description(description)
                    .termsOfServiceUrl(termsOfServiceUrl)
                    .contact(new Contact(contactName, contactUrl, contactEmail))
                    .version(version)
                    .build();
        }

    }

